from pydub import AudioSegment
import os
import subprocess
from pydub.playback import play

def play_audio(file_path):
    try:
        audio = AudioSegment.from_file(file_path)
        duration_in_milliseconds = len(audio)
        duration_in_seconds = duration_in_milliseconds / 1000.0
        print(f"音频时长为: {duration_in_seconds}秒")

        # play(audio)

    except Exception as e:
        print(f"出现错误: {e}")


# 获取当前工作目录
current_dir = os.getcwd()
# 用于存储符合条件的文件名的列表
m4a_files = []

# 遍历当前目录下的所有文件和文件夹
for root, dirs, files in os.walk(current_dir):
    for file in files:
        if file.endswith('.m4a'):
            m4a_files.append(os.path.join(root, file))

# 打印出所有符合条件的文件名
for m4a_file in m4a_files:
    print(m4a_file)

    # file_path = "001.m4a"  # 替换为实际的音频文件路径
    play_audio(m4a_file)